package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisPrescriptionDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.moudle.HisPrescription;
import com.woniu.mapper.HisPrescriptionMapper;
import com.woniu.param.AddByIdHisPrescriptionParam;
import com.woniu.param.AddHisPrescriptionParam;
import com.woniu.param.ListHisPrescriptionParam;
import com.woniu.param.ModifyHisPrescriptionParam;
import com.woniu.service.HisPrescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w
 * @since 2021-11-11
 */
@Service
public class HisPrescriptionServiceImpl extends ServiceImpl<HisPrescriptionMapper, HisPrescription> implements HisPrescriptionService {
    @Resource
    private HisPrescriptionMapper hisPrescriptionMapper;

    @Override
    public List<HisPrescriptionDto> newDrugs(AddByIdHisPrescriptionParam addByIdHisPrescriptionParam) {
        List<AddHisPrescriptionParam> list = addByIdHisPrescriptionParam.getList();
        HisPrescription hisPrescription = new HisPrescription();
        QueryWrapper<HisPrescription> hisPrescriptionQueryWrapper = new QueryWrapper<>();

        BeanUtils.copyProperties(addByIdHisPrescriptionParam,hisPrescription);
        ArrayList<HisPrescriptionDto> hisPrescriptionDtos = new ArrayList<>();
        for (AddHisPrescriptionParam addHisPrescriptionParam : list) {
            BeanUtils.copyProperties(addHisPrescriptionParam,hisPrescription);
            hisPrescriptionMapper.insert(hisPrescription);
            HisPrescriptionDto hisPrescriptionDto = new HisPrescriptionDto();
            BeanUtils.copyProperties(hisPrescription,hisPrescriptionDto);
            hisPrescriptionDtos.add(hisPrescriptionDto);
        }
        return hisPrescriptionDtos;
    }

    @Override
    public Boolean deleteMedication(ArrayList<Integer> ids) {
        int i = hisPrescriptionMapper.deleteBatchIds(ids);
        return i>0?true:false;
    }

    @Override
    public Boolean modifyState(ModifyHisPrescriptionParam modifyHisPrescriptionParam) {
        UpdateWrapper<HisPrescription> hisPrescriptionUpdateWrapper = new UpdateWrapper<>();
        if(modifyHisPrescriptionParam.getCareId()!=null){
            hisPrescriptionUpdateWrapper.eq("care_id",modifyHisPrescriptionParam.getCareId());
        }else{
            hisPrescriptionUpdateWrapper.eq("order_form_id",modifyHisPrescriptionParam.getOrderFormId());
        }
        hisPrescriptionUpdateWrapper.set(true,"prescription_isreceive",1);
        boolean update = update(hisPrescriptionUpdateWrapper);
        if(!update){
            throw new DIYException(-9999,"修改失败");
        }
        return true;
    }

    @Override
    public Boolean modifyPayment(ModifyHisPrescriptionParam modifyHisPrescriptionParam) {
        UpdateWrapper<HisPrescription> hisPrescriptionUpdateWrapper = new UpdateWrapper<>();
        if(modifyHisPrescriptionParam.getCareId()!=null){
            hisPrescriptionUpdateWrapper.eq("care_id",modifyHisPrescriptionParam.getCareId());
        }else{
            hisPrescriptionUpdateWrapper.eq("order_form_id",modifyHisPrescriptionParam.getOrderFormId());
        }
        hisPrescriptionUpdateWrapper.set(true,"prescription_ispay",1);
        boolean update = update(hisPrescriptionUpdateWrapper);
        if(!update){
            throw new DIYException(-9999,"修改失败");
        }
        return true;
    }

    @Override
    public PageDTO queryMedication(ListHisPrescriptionParam listHisPrescriptionParam) {
        QueryWrapper<HisPrescription> hisPrescriptionQueryWrapper = new QueryWrapper<>();
        if(listHisPrescriptionParam!=null){
            if(listHisPrescriptionParam.getCareId()!=null){
                hisPrescriptionQueryWrapper.eq("care_id",listHisPrescriptionParam.getCareId());
            }
            if(listHisPrescriptionParam.getEndTime()!=null){
                hisPrescriptionQueryWrapper.eq("end_time",listHisPrescriptionParam.getEndTime());
            }
            if(listHisPrescriptionParam.getMedicineId()!=null){
                hisPrescriptionQueryWrapper.eq("medicine_id",listHisPrescriptionParam.getMedicineId());
            }
            if(listHisPrescriptionParam.getMedicineName()!=null){
                hisPrescriptionQueryWrapper.eq("medicine_name",listHisPrescriptionParam.getMedicineName());
            }
            if(listHisPrescriptionParam.getOrderFormId()!=null){
                hisPrescriptionQueryWrapper.eq("order_form_id",listHisPrescriptionParam.getOrderFormId());
            }
            if(listHisPrescriptionParam.getPrescriptionCompany()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_company",listHisPrescriptionParam.getPrescriptionCompany());
            }
            if(listHisPrescriptionParam.getPrescriptionId()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_id",listHisPrescriptionParam.getPrescriptionId());
            }
            if(listHisPrescriptionParam.getPrescriptionIspay()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_ispay",listHisPrescriptionParam.getPrescriptionIspay());
            }
            if(listHisPrescriptionParam.getPrescriptionIsreceive()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_isreceive",listHisPrescriptionParam.getPrescriptionIsreceive());
            }
            if(listHisPrescriptionParam.getPrescriptionNum()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_num",listHisPrescriptionParam.getPrescriptionNum());
            }
            if(listHisPrescriptionParam.getPrescriptionPrice()!=null){
                hisPrescriptionQueryWrapper.eq("prescription_price",listHisPrescriptionParam.getPrescriptionPrice());
            }
            if(listHisPrescriptionParam.getStartTime()!=null){
                hisPrescriptionQueryWrapper.eq("start_time",listHisPrescriptionParam.getStartTime());
            }
        }
        Page<HisPrescription> page=page(new Page<HisPrescription>(listHisPrescriptionParam.getPageParam().getPage(),listHisPrescriptionParam.getPageParam().getPageSize()),hisPrescriptionQueryWrapper);
        return PageDTO.bulid(page,HisPrescriptionDto.class);
    }
}
